/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.User;

import Model.DataBase;
import RssEasy.Core.CookieHelper;
import RssEasy.Core.Encrypt;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

/**
 *
 * @author Administrator
 */
public class UserList extends DataBase {
    public UserList(PageContext out) throws Exception {
        super(out, "user_list");
    }
  public static String MyID(HttpServletRequest request) {
        CookieHelper cookie = new CookieHelper(request, null);
        return cookie.Get("myid");
    }

    public static String RealName(HttpServletRequest request) {
        CookieHelper cookie = new CookieHelper(request, null);
        return cookie.Get("account");
        
    }
    
    public boolean login() throws Exception {
        String pwd = Encrypt.Md532(this.get("pwd")+this.get("account"));
        if (this.select("account,pwd").where("account=?", new String[]{this.get("account")}).get_first_rows()) {
            if (!pwd.equals(this.get("pwd"))) {
                throw new Exception("2");
            }

            CookieHelper cookie = new CookieHelper(this.req, this.resp);
            cookie.EncryptSet("myid", this.get("myid"));
            cookie.EncryptSet("account", this.get("account"));
            cookie.Set("myid", this.get("myid"));
            cookie.Set("account", this.get("account"));
            return true;
        }
        throw new Exception("1");
    }
    public static void IsLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {

        CookieHelper cookie = new CookieHelper(request, response);
        if (cookie.EncryptGet("account") == null) {
            response.sendRedirect("/login/login.jsp");
            response.flushBuffer();
        }
    }
    
}
